package com.cykj.pos.service.impl;

import com.cykj.pos.domain.*;
import com.cykj.pos.domain.dto.BizEntrySnDTO;

import com.cykj.pos.domain.dto.BizTransactionCountDTO;
import com.cykj.pos.domain.posone.BizAreaCodeDTO;
import com.cykj.pos.domain.posone.BizAreaCodeInfoDTO;
import com.cykj.pos.domain.posone.BizMerchantEntryDTO;
import com.cykj.pos.domain.posone.PosOneMerchantInfo;
import com.cykj.pos.profit.dto.MerchantDTO;
import com.cykj.pos.service.IBizMerchantService;
import com.cykj.pos.util.DateUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import com.cykj.pos.mapper.BizMerchantEntryMapper;
import com.cykj.pos.service.IBizMerchantEntryService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 1、统一商户入件Service业务层处理
 *
 * @author luo
 * @date 2021-08-31
 */
@Service
public class BizMerchantEntryServiceImpl extends ServiceImpl<BizMerchantEntryMapper, BizMerchantEntry> implements IBizMerchantEntryService {

    @Resource
    private IBizMerchantService bizMerchantService;

    @Resource
    private BizMerchantEntryMapper bizMerchantEntryMapper;

    @Override
    public List<BizMerchantEntry> queryList(BizMerchantEntry bizMerchantEntry) {
        LambdaQueryWrapper<BizMerchantEntry> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(bizMerchantEntry.getPhone())){
            lqw.eq(BizMerchantEntry::getPhone ,bizMerchantEntry.getPhone());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getApplicant())){
            lqw.eq(BizMerchantEntry::getApplicant ,bizMerchantEntry.getApplicant());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getProvinceno())){
            lqw.eq(BizMerchantEntry::getProvinceno ,bizMerchantEntry.getProvinceno());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getCityno())){
            lqw.eq(BizMerchantEntry::getCityno ,bizMerchantEntry.getCityno());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getAreano())){
            lqw.eq(BizMerchantEntry::getAreano ,bizMerchantEntry.getAreano());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getCertificateno())){
            lqw.eq(BizMerchantEntry::getCertificateno ,bizMerchantEntry.getCertificateno());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getCertificatestartdate())){
            lqw.eq(BizMerchantEntry::getCertificatestartdate ,bizMerchantEntry.getCertificatestartdate());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getCertificateenddate())){
            lqw.eq(BizMerchantEntry::getCertificateenddate ,bizMerchantEntry.getCertificateenddate());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getBankcardaccount())){
            lqw.eq(BizMerchantEntry::getBankcardaccount ,bizMerchantEntry.getBankcardaccount());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getFeeChlId())){
            lqw.eq(BizMerchantEntry::getFeeChlId ,bizMerchantEntry.getFeeChlId());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getCardId())){
            lqw.eq(BizMerchantEntry::getCardId ,bizMerchantEntry.getCardId());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getValiddate())){
            lqw.eq(BizMerchantEntry::getValiddate ,bizMerchantEntry.getValiddate());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getMerchantNo())){
            lqw.eq(BizMerchantEntry::getMerchantNo ,bizMerchantEntry.getMerchantNo());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getSn())){
            lqw.eq(BizMerchantEntry::getSn ,bizMerchantEntry.getSn());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getAccountId())){
            lqw.eq(BizMerchantEntry::getAccountId ,bizMerchantEntry.getAccountId());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getMersource())){
            lqw.eq(BizMerchantEntry::getMersource ,bizMerchantEntry.getMersource());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getIdcardhand())){
            lqw.eq(BizMerchantEntry::getIdcardhand ,bizMerchantEntry.getIdcardhand());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getIdcardfront())){
            lqw.eq(BizMerchantEntry::getIdcardfront ,bizMerchantEntry.getIdcardfront());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getIdcardback())){
            lqw.eq(BizMerchantEntry::getIdcardback ,bizMerchantEntry.getIdcardback());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getBankcardfront())){
            lqw.eq(BizMerchantEntry::getBankcardfront ,bizMerchantEntry.getBankcardfront());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getBankcardback())){
            lqw.eq(BizMerchantEntry::getBankcardback ,bizMerchantEntry.getBankcardback());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getAgentid())){
            lqw.eq(BizMerchantEntry::getAgentid ,bizMerchantEntry.getAgentid());
        }
        if (StringUtils.isNotBlank(bizMerchantEntry.getFacerecognizeface())){
            lqw.eq(BizMerchantEntry::getFacerecognizeface ,bizMerchantEntry.getFacerecognizeface());
        }
        return this.list(lqw);
    }

    @Override
    public int queryMyMerchNum(Long userId) {
        BizMerchant bizMerchant = bizMerchantService.getMerchantByUserId(userId);
        List<PosOneMerchantInfo> list =bizMerchantEntryMapper.queryMyMerchNum(bizMerchant.getMerchId());
        return list.size();
    }

    @Override
    public List<PosOneMerchantInfo> getPosOneMerchantInfo(MerchantDTO merchantDTO) {
        merchantDTO.setQueryMonth(DateUtils.dateFormatStringToo(new Date()));
        if(merchantDTO.getPageNo()!=null && merchantDTO.getPageSize()!=null){
            Integer pageNo = merchantDTO.getPageNo();
            Integer pageSize = merchantDTO.getPageSize();
            Integer start = (pageNo-1)*pageSize;
            merchantDTO.setStart(start);
        }
        Long userId = merchantDTO.getUserId();
        BizMerchant bizMerchant = bizMerchantService.getMerchantByUserId(userId);
        // 查询后缀
        String suffix = bizMerchantEntryMapper.selectSuffix(userId);
        String tableName ="";
        // 拼接机构表名称
        if(StringUtils.isNotBlank(suffix)){
            tableName="biz_transaction_amount_" + suffix;
        }else{
            tableName="biz_transaction_amount";
        }
        merchantDTO.setMerchId(bizMerchant.getMerchId());
        List<PosOneMerchantInfo> list = bizMerchantEntryMapper.getPosOneMerchantInfo(merchantDTO);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setQueryMonth(DateUtils.dateFormatStringToo(new Date()).substring(0, 7));
            list.get(i).setTableName(tableName);
            PosOneMerchantInfo pos = bizMerchantEntryMapper.getPosOneMerchantInfoAmount(list.get(i));
            list.get(i).setTransAmount(pos.getTransAmount());
        }
//        List<PosOneMerchantInfo> listOver = new ArrayList<PosOneMerchantInfo>();
//        if (merchantDTO.getOrderly()==1){
//            listOver = ListUtils.searchPosOneAsc(list);
//        } else if (merchantDTO.getOrderly()==2){
//            listOver = ListUtils.searchPosOneDes(list);
//        }
        return list;
    }

    /**
     * 查询省市区编码
     * @param bizAreaCodeDTO
     * @return
     */
    @Override
    public List<BizAreaCodeDTO> queryRegionalCode(BizAreaCodeDTO bizAreaCodeDTO) {
        return bizMerchantEntryMapper.queryRegionalCode(bizAreaCodeDTO);
    }

    @Override
    public BizMerchantEntry getBizMerchantEntryBySn(String snCode) {
        return bizMerchantEntryMapper.getBizMerchantEntryBySn(snCode);
    }

    @Override
    public BizMerchantEntry getBizMerchantEntryBySns(String snCode) {
        return bizMerchantEntryMapper.getBizMerchantEntryBySns(snCode);
    }

    @Override
    public BizMerchantEntry getBizMerchantEntryByMerchantNo(String merchantNo) {
        return bizMerchantEntryMapper.getBizMerchantEntryByMerchantNo(merchantNo);
    }

    @Override
    public BizEntrySnDTO getBizEntrySn(String snCode) {
        return bizMerchantEntryMapper.getBizEntrySn(snCode);
    }
    @Override
    public BizMerchantEntry selectBizMerchantEntry(Long id) {
        return bizMerchantEntryMapper.selectBizMerchantEntry(id);
    }
    @Override
    public int selectBizEntrySnNum(Long id) {
        return bizMerchantEntryMapper.selectBizEntrySnNum(id);
    }
    @Override
    public int upDateBizMerchantEntry(BizMerchantEntry bizMerchantEntryupdate) {
        return bizMerchantEntryMapper.upDateBizMerchantEntry(bizMerchantEntryupdate);
    }

    /**
     * 查询报件信息
     * @param merchId
     * @return
     */
    @Override
    public List<BizMerchantEntry> getBizMerchantEntryByMerchId(Long merchId) {
        return bizMerchantEntryMapper.getBizMerchantEntryByMerchId(merchId);
    }

    /**
     * 修改商户详情信息
     * @param snCode
     * @return
     */
    @Override
    public BizMerchantEntryDTO getBizMerchantEntryInfoBySn(String snCode) {
        return bizMerchantEntryMapper.getBizMerchantEntryInfoBySn(snCode);
    }

    @Override
    public BizMerchantEntryDTO getBizMerchantEntryInfoByByMerchantNo(String merchantNo) {
        return bizMerchantEntryMapper.getBizMerchantEntryInfoByByMerchantNo(merchantNo);
    }

    @Override
    public BizAreaCodeInfoDTO getBizAreaCodeByCode(String provinceno) {
        return bizMerchantEntryMapper.getBizAreaCodeByCode(provinceno);
    }

    /**
     * 获得feeId
     * @param feeChlId
     * @return
     */
    @Override
    public BizAreaCodeInfoDTO getFeeIdInfo(String feeChlId) {
        return bizMerchantEntryMapper.getFeeIdInfo(feeChlId);
    }

    /**
     * 更改报件信息
     * @param bizMerchantEntry
     * @return
     */
    @Override
    public int updateizMerchantEntry(BizMerchantEntry bizMerchantEntry) {
        return bizMerchantEntryMapper.updateizMerchantEntry(bizMerchantEntry);
    }

    @Override
    public String findMerchantNo(String snCode) {
        return bizMerchantEntryMapper.findMerchantNo(snCode);
    }

    @Override
    public void updateMerchantEntry(String merchantNo, String proMerSub) {
        bizMerchantEntryMapper.updateMerchantEntry(merchantNo, proMerSub);
    }

    @Override
    public void insertBizEntrySn(BizMerchantEntry bizMerchantEntry) {
        bizMerchantEntryMapper.insertBizEntrySn(bizMerchantEntry);
    }

    @Override
    public Long selectBizMerchantEntryId(String sn) {
        return bizMerchantEntryMapper.selectBizMerchantEntryId(sn);
    }

    @Override
    public List<BizTransactionCountDTO> selectBizMerchantEntryByMerchantNo(BizMerchantEntry bizMerchantEntry) {
        return bizMerchantEntryMapper.selectBizMerchantEntryByMerchantNo(bizMerchantEntry);
    }

    @Override
    public BizTransactionCountDTO selectBizMerchantEntryMerchNameAndTime(String merchantNo) {
        return bizMerchantEntryMapper.selectBizMerchantEntryMerchNameAndTime(merchantNo);
    }

    @Override
    public String selectBizMerchantEntrySn(Long id) {
        return bizMerchantEntryMapper.selectBizMerchantEntrySn(id);
    }

    @Override
    public int upDateBizMerchantEntryMerchantNo(BizMerchantEntry bizMerchantEntry) {
      return   bizMerchantEntryMapper.upDateBizMerchantEntryMerchantNo(bizMerchantEntry);
    }

    @Override
    public Long setBizPlatformSync(Long userId) {
      return   bizMerchantEntryMapper.setBizPlatformSync(userId);
    }

    @Override
    public void updateizMerchantEntryFacerecognizeface(BizMerchantEntry bizMerchantEntry1) {
        bizMerchantEntryMapper.updateizMerchantEntryFacerecognizeface(bizMerchantEntry1);
    }

    @Override
    public void deleteMerchantEntry(Long id ,String snCode) {
        bizMerchantEntryMapper.deleteMerchantEntry(id);
      //  bizMerchantEntryMapper.deleteMerchantEntrySn(snCode);
    }

    @Override
    public void updateizMerchantEntryErroMessage(Long id,String retMsge) {
        bizMerchantEntryMapper.updateizMerchantEntryErroMessage(id,retMsge);
    }

    @Override
    public int insterRuleResolution(BizPlatformIncomingParts bizPlatformIncomingParts) {
        bizPlatformIncomingParts.setCreateTime(com.cykj.common.utils.DateUtils.getNowDate());
        return bizMerchantEntryMapper.insterRuleResolution(bizPlatformIncomingParts);
    }
    @Override
    public List<BizEntryIndustry> getIndustry() {
        return bizMerchantEntryMapper.getIndustry();
    }
}
